package com.fitbet.admin.web.controller.authority;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.fitbet.admin.entity.principal.Principal;
import com.fitbet.admin.entity.security.Login;
import com.fitbet.admin.service.principal.PrincipalService;
import com.fitbet.admin.web.resolver.Secure;
/**
 * 个人中心
 * @author sjh
 *
 */
@Controller
@RequestMapping("/profile")
@MenuSetting("menu-authority")
public class ProfileController {
	
	public static final Logger logger = LoggerFactory.getLogger(ProfileController.class);
	
	@Autowired
	private PrincipalService principalService;
	
	/**
	 * 个人中心
	 * @param login
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	@MenuSetting("submenu-profile-info")
	public ModelAndView profile(@Secure Login login) {
		
		ModelAndView modelAndView = new ModelAndView("authority/profile/profile");
		
		Principal profile = principalService.load(Principal.class, login.getId());

		modelAndView.addObject("id", login.getId());
		
		modelAndView.addObject("profile", profile);
		
		return modelAndView;
	}
	
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/change-password", method = RequestMethod.GET)
	public ModelAndView changePassword(@PathVariable("id") Long id) {
		
		ModelAndView modelAndView = new ModelAndView("authority/profile/change-password");
		
		modelAndView.addObject("id", id);
		
		return modelAndView;
	}
	
	/**
	 * 
	 * @param id
	 * @param password
	 * @return
	 */
	
	@RequestMapping(value = "/{id}/change-password", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public HttpResult onChangePassword(@PathVariable("id") Long id, @RequestParam("password") String password) {
		
		HttpResult httpResult = new HttpResult();
		
		try {

            Principal principal = principalService.load(Principal.class, id);
            principalService.changePassword(principal, password);
            httpResult.success().message("修改密码成功！");
            
        } catch (BusinessException e) {
            
            httpResult.fail("500").message(e.getMessage());
            
        } catch (Exception e) {
            
            httpResult.fail("500").message("修改密码失败，请稍后再试！");
            
            logger.error("onChangePassword() error", e);
            
        }
		
		return httpResult;
	}

}
